package com.golang.activity.function;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.IdUtil;
import com.golang.activity.dto.request.*;
import com.golang.activity.dto.response.*;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.domain.ActivityProcess;
import com.ruoyi.system.domain.ActivityTag;
import com.ruoyi.system.domain.ActivityTicket;

import java.time.LocalDateTime;
import java.util.function.Function;

public interface ActivityFunction {

    //创建活动基本信息
    default Function<ActivityBasicDto, Activity> activityBasic(ActivityBasicDto dto) {
        return a -> {
            Activity activity = new Activity();
            activity.setActivityId("HD" + IdUtil.getSnowflake().nextIdStr()); //活动编码
            activity.setActivityTitle(dto.getActivityTitle());  //活动标题
            activity.setActivityIntroduction(dto.getActivityIntroduction()); //活动详情
            activity.setActivityType(dto.getActivityType());
            activity.setActivityClassification(dto.getActivityClassification().longValue());
            activity.setActivityAddress(dto.getActivityAddress());

            LocalDateTime parseStart = LocalDateTime.parse(dto.getStartTime(), DatePattern.NORM_DATE_FORMATTER);
            LocalDateTime parseEnd = LocalDateTime.parse(dto.getEndTime(), DatePattern.NORM_DATETIME_FORMATTER);

            if (LocalDateTime.now().compareTo(parseStart) < 0) {
                activity.setActivityStatus(0l);
            } else if (LocalDateTime.now().compareTo(parseEnd) <= 0 && LocalDateTime.now().compareTo(parseStart) >= 0) {
                activity.setActivityStatus(1l);
            } else if (LocalDateTime.now().compareTo(parseEnd) >= 0) {
                activity.setActivityStatus(2l);
            }
            //activity.setStartTime(Date.from(parseStart.atZone(ZoneId.systemDefault()).toInstant()));
            activity.setStartTime(parseStart);
            activity.setEndTime(parseEnd);
            return activity;
        };
    }


    default Function<Activity, ActivityBasicRespDto> ActivityBasic2Dto(Activity entity) {

        return dto -> new ActivityBasicRespDto()
                .setActivityId(entity.getActivityId())
                .setActivityTitle(entity.getActivityTitle())
                .setActivityIntroduction(entity.getActivityIntroduction())
                .setActivityClassification(entity.getActivityClassification().intValue())
                .setActivityType(entity.getActivityType())
                .setActivityAddress(entity.getActivityAddress())
                .setStartTime(entity.getStartTime().format(DatePattern.NORM_DATETIME_FORMATTER))
                .setEndTime(entity.getEndTime().format(DatePattern.NORM_DATETIME_FORMATTER));


    }


    //创建活动报名设置
    default Function<ActivitySettingDto, Activity> activitySetting(ActivitySettingDto dto, Activity activity) {
        return a -> {
            activity.setCommunityId(dto.getCommunityId());
            activity.setQuestion(dto.getQuestion()); //问题
            activity.setPrivacyType(dto.getPrivacyType()); //隐私性
            activity.setManagerPhone(dto.getManagerPhone());
            activity.setManagerWechat(dto.getManagerWechat());
            activity.setManagerWechatGroupPic(dto.getManagerWechatGroupPic());
            return activity;
        };
    }


    default Function<Activity, ActivitySettingRespDto> ActivitySetting2Dto(Activity entity) {

        return dto -> new ActivitySettingRespDto()
                .setActivityId(entity.getActivityId())
                .setCommunityId(entity.getCommunityId())
                .setQuestion(entity.getQuestion())
                .setManagerPhone(entity.getManagerPhone())
                .setManagerWechat(entity.getManagerWechat())
                .setManagerWechatGroupPic(entity.getManagerWechatGroupPic())
                .setPrivacyType(entity.getPrivacyType());


    }


    //创建活动流程信息
    default Function<ActivityProcessDto, ActivityProcess> activityProcessDto2ActivityProcess(ActivityProcessDto dto, String activityId) {
        return entity -> {
            ActivityProcess activityProcess = new ActivityProcess();
            activityProcess.setActivityId(activityId);
            activityProcess.setProcessTime(LocalDateTime.parse(dto.getProcessTime(), DatePattern.NORM_DATE_FORMATTER));

            return activityProcess;
        };
    }


    default Function<ActivityProcess, ActivityProcessRespDto> ActivityProcess2Dto(ActivityProcess entity) {
        return dto -> new ActivityProcessRespDto()
                .setId(entity.getId())
                .setProcessTime(entity.getProcessTime().format(DatePattern.NORM_DATETIME_FORMATTER))
                .setProcessContent(entity.getProcessContent());
    }


    default Function<ActivityTag, ActivityTagRespDto> ActivityTag2Dto(ActivityTag entity) {
        return dto -> new ActivityTagRespDto()
                .setTagId(entity.getTagId())
                .setTagName(entity.getTagName());
    }

    default Function<ActivityTicket, ActivityTicketRespDto> ActivityProcess2Dto(ActivityTicket entity) {
        return dto -> new ActivityTicketRespDto()
                .setTicketName(entity.getTicketName())
                .setPrice(entity.getPrice())
                .setEnrollMaxCount(entity.getEnrollMaxCount())
                .setLimitQuantity(entity.getLimitQuantity())
                .setBuyDeadline(entity.getBuyDeadline().format(DatePattern.NORM_DATE_FORMATTER));
    }


    default Function<ActivityTicketDto, ActivityTicket> activityTicketDto2ActivityTicket(ActivityTicketDto dto, String activityId, String refundDeadline) {
        return entity -> {
            ActivityTicket activityTicket = new ActivityTicket();
            activityTicket.setActivityId(activityId);
            activityTicket.setTicketName(dto.getTicketName());
            //activityTicket.setTicketType(dto.get);
            activityTicket.setPrice(dto.getPrice());
            activityTicket.setEnrollMaxCount(dto.getEnrollMaxCount());
            activityTicket.setLimitQuantity(dto.getLimitQuantity());
            activityTicket.setBuyDeadline(LocalDateTime.parse(dto.getBuyDeadline(), DatePattern.NORM_DATE_FORMATTER));
            activityTicket.setRefundDeadline(LocalDateTime.parse(refundDeadline, DatePattern.NORM_DATE_FORMATTER));

            return activityTicket;
        };
    }


    default Function<ActivityTagDto, ActivityTag> activityTagDto2ActivityTag(ActivityTagDto dto, String activityId) {
        return entity -> {
            ActivityTag activityTag = new ActivityTag();
            activityTag.setActivityId(activityId);
            activityTag.setTagId(dto.getTagId());
            activityTag.setTagName(dto.getTagName());

            return activityTag;
        };
    }


}
